using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using StarlightEngine;

namespace StarlightEngine
{
    public class MainScene : Scene
    {
        TouchLocation lastTouch;
        Vector2 currenTouch = Vector2.Zero;

        Vector2 lastDistance;
        Vector2 distance;

        bool wasZoomLast = false;
        AwesomeButton b;
        public MainScene()
        {
            //Load
            this.LoadFromFile("Content/Scenes/MainScene.zhds");
            //Add some text.
            b = new AwesomeButton();
            this.AddNode("TextButton", b);
            b.ButtonDown += new TextOnlyButtonNode.ButtonActivatedEvent(TouchEvent);
        }

        public void TouchEvent(Object o, EventArgs e)
        {            
            foreach (Node n in this.Nodes.Values)
            {
                Color t = n.DrawColor;
                t.A -= 20;
                n.DrawColor = t;
            }
        }

        public override void Update()
        {
            lastDistance = distance;
            b.Text = "Press Me";
#if ZUNE
            if (InputManager.Touch.TotalTouches == 1)
            {
                wasZoomLast = false;
                //We'll move.
                TouchLocation touch = InputManager.Touch.GetTouch(0);

                if (touch.State == TouchLocationState.Moved)
                {
                    currenTouch = touch.Position;
                    touch.TryGetPreviousLocation(out lastTouch);
                    Vector2 difference = currenTouch - lastTouch.Position;
                    this.Move(-difference);
                }
            }
            else if (InputManager.Touch.TotalTouches == 2)
            {
                //We'll Zoom.
                TouchLocation touch1 = InputManager.Touch.GetTouch(0);
                TouchLocation touch2 = InputManager.Touch.GetTouch(1);
                distance = touch1.Position - touch2.Position;

                if (!wasZoomLast)
                {
                    lastDistance = distance;
                }

                Vector2 dif = lastDistance - distance;
                //System.Diagnostics.Debug.WriteLine(dif);

                float zoomAmount = dif.Y / 64;
                this.Zoom(zoomAmount);
                wasZoomLast = true;

            }
            else
                wasZoomLast = false;

#endif
           
            base.Update();
        }
    }

    public class AwesomeButton : TextOnlyButtonNode
    {
        public AwesomeButton()
            : base("Engine.Font", "Touch Me.")
        {

        }

        public override void Touched()
        {
            //System.Diagnostics.Debug.WriteLine("Touched me!");
            base.Touched();
        }
    
    
    }
}